import { Component, OnInit } from '@angular/core';
import {Response} from "@angular/http";
import {UserService} from "../user/user.service";

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {

  images: any[];

  constructor(
    private userService:UserService,
  ) { }

  ngOnInit() {
    this.loadImages();
    this.loadFileInfos();
  }

  firstImag:any;
  loadImages(){
    this.images = [];
    let homeResourceInfo = {
      properties:{type:0,status:1}
    }
    this.userService.getImageList(homeResourceInfo)
      .subscribe((res:Response)=>{
        let data = res.json();
        if (data.success){
          console.log("data:",data);
          let temp = [];
          let tempFirst = {source:'../assets/images/'+data.value[0].code+'.png',url:data.value[0].url};
          if(data.value.length>1){
            for(let i=1;i<data.value.length;i++){
              temp.push({source:'../assets/images/'+data.value[i].code+'.png',url:data.value[i].url,index:i})
            }
          }
          this.firstImag = tempFirst;
          this.images = temp;
        }
      });
    console.log("images:",this.images);
  }

  fileInfos:any;
  loadFileInfos(){
    let homeResourceInfo = {
      properties:{type:1,status:1}
    }
    this.userService.getImageList(homeResourceInfo)
      .subscribe((res:Response)=>{
        let data = res.json();
        if (data.success){
          console.log("data:",data);
          this.fileInfos = data.value;
        }
      });
  }

  openUrl(data){
    console.log("event:",data);
    let url = data.url;
    window.open(url);
  }

}
